package com.example.zzy.zzytest.data.structure.list.doublelinkedlist;

public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        HeroNode heroNode1 = new HeroNode(1, "宋江", "及时雨");
        HeroNode heroNode2 = new HeroNode(2, "卢俊义", "玉麒麟");
        HeroNode heroNode3 = new HeroNode(3, "吴用", "智多星");
        HeroNode heroNode4 = new HeroNode(4, "林冲", "豹子头");

        DoubleLinkedList list = new DoubleLinkedList();
        list.add(heroNode1);
        list.add(heroNode2);
        list.add(heroNode3);
        list.add(heroNode4);
        list.show();
        System.out.println("\n");

        list.update(new HeroNode(2, "鲁智深俊逸", "花姑娘"));
        list.show();
        System.out.println("\n");

        list.delete(4);
        list.delete(2);
        list.show();
        list.delete(2);
    }
}

class HeroNode {
    public int no;
    public String name;
    public String nickName;
    public HeroNode pre;
    public HeroNode next;

    public HeroNode(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}

class DoubleLinkedList {
    //头结点不动，不放具体值
    private HeroNode head = new HeroNode(0, "", "");

    //遍历
    public void show() {
        if (head.next == null) {
            System.out.println("链表为空 ");
            return;
        }
        HeroNode cur = head.next;
        while (cur != null) {
            System.out.println(cur);
            cur = cur.next;
        }
    }

    //添加一个节点到双向链表的最后
    public void add(HeroNode node) {
        HeroNode cur = head;
        while (true) {
            if (cur.next == null) {
                break;
            }
            cur = cur.next;
        }
        cur.next = node;
        node.pre = cur;
    }

    //更新
    public void update(HeroNode node) {
        if (head.next == null) {
            return;
        }
        HeroNode cur = head.next;
        boolean flag = false;
        while (cur != null) {
            if (cur.no == node.no) {
                flag = true;
                break;
            }
            cur = cur.next;
        }

        if (flag) {
            cur.name = node.name;
            cur.nickName = node.nickName;
        } else {
            System.out.println("未找到该节点");
        }
    }

    //删除
    public void delete(int no) {
        if (head.next == null) {
            return;
        }
        HeroNode cur = head.next;
        while (cur != null) {
            if (cur.no == no) {
                //删除该节点
                //需要保证双.的正确性，即.pre.next和.next.pre考虑空的情况
                //.next 对于最后一个节点来说是空，在.pre会报错
                cur.pre.next = cur.next;
                if (cur.next != null) {
                    cur.next.pre = cur.pre;
                }
                System.out.println("已删除no==" + no);
                break;
            }
            cur = cur.next;
        }
        if (cur == null) {
            System.out.println("未能找到该节点no==" + no);
        }
    }
}